package com.qingxin.wuye.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyGarden;

@Service
public class WyGardenService extends BaseService<WyGarden, String> {

	/**
	 * 获取菜单树数据
	 * @param companyid
	 * @param loginUser
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeData(String companyid, LoginUser loginUser, boolean isParent){
		List<JsTreeNode> list = new ArrayList<>();
		if(loginUser == null){
			return list;
		}
		if(!SystemCfg.isAdminUser(loginUser)){
			companyid = loginUser.getCompanyid();
		}
		
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.wuye.entity.WyGarden(p.gardenid, p.gardenName)");
		hql.append(" from WyGarden p where 1=1 ");
		if(StringUtils.isNotEmpty(companyid) && !"0".equals(companyid)){
			hql.append(" and p.wyPropertyCompany.companyid='"+companyid.replace("'", "")+"' ");
		}
		if(!SystemCfg.isDataAdminUser(loginUser)){
			hql.append(" and p.gardenid in (select distinct v.wyGarden.gardenid from SysUserGardenRel v where v.sysUser.userid='"+loginUser.getUserid()+"')");
		}
		hql.append(" order by p.gardenName asc");
		List<WyGarden> gardens = listByHql(hql.toString());
		if(gardens != null){
			for(WyGarden garden : gardens){
				list.add(new JsTreeNode(garden.getGardenid(), garden.getGardenName(), SystemCfg.UI_DATATYPE_GARDEN, "gardenid", isParent));
			}
		}
		return list;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeDataByCity(String cityid, LoginUser loginUser, boolean isParent){
		List<JsTreeNode> list = new ArrayList<>();
		if(loginUser == null){
			return list;
		}
		
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.wuye.entity.WyGarden(p.gardenid, p.gardenName)");
		hql.append(" from WyGarden p where 1=1 ");
		
		if(StringUtils.isNotEmpty(cityid) && !"0".equals(cityid)){
			hql.append(" and p.cityid='"+cityid.replace("'", "")+"' ");
		}
		
		if(!SystemCfg.isDataAdminUser(loginUser)){
			hql.append(" and p.gardenid in (select distinct v.wyGarden.gardenid from SysUserGardenRel v where v.sysUser.userid='"+loginUser.getUserid()+"')");
		}
		hql.append(" order by p.gardenName asc");
		List<WyGarden> gardens = listByHql(hql.toString());
		if(gardens != null){
			for(WyGarden garden : gardens){
				list.add(new JsTreeNode(garden.getGardenid(), garden.getGardenName(), SystemCfg.UI_DATATYPE_GARDEN, "gardenid", isParent));
			}
		}
		return list;
	}
	
	
	/**
	 * 获取小区列表分页数据
	 * @param parkName
	 * @param parkStatus
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String companyid, String gardenName, PageParam pageParam, LoginUser loginUser){
		if(!SystemCfg.isAdminUser(loginUser)){
			companyid = loginUser.getCompanyid();
		}
		
		Querier querier = Querier.create("from WyGarden p where 1=1 ");
		if(StringUtils.isNotEmpty(companyid) && !"0".equals(companyid)){
			querier.append(" and p.wyPropertyCompany.companyid= :companyid ");
			querier.setParam("companyid", companyid);
		}
		if(!SystemCfg.isDataAdminUser(loginUser)){
			querier.append(" and p.gardenid in (select distinct v.wyGarden.gardenid from SysUserGardenRel v where v.sysUser.userid=:userid)");
			querier.setParam("userid", loginUser.getUserid());
		}
		if(StringUtils.isNotEmpty(gardenName)){
			gardenName = gardenName.replace("'", "");
			querier.append(" and (p.gardenName like :gardenName) ");
			querier.setParam("gardenName", "%"+gardenName+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by p.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	public Pager listByCity(String cityid, String gardenName, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from WyGarden p where 1=1 ");
		if(StringUtils.isNotEmpty(cityid) && !"0".equals(cityid)){
			querier.append(" and p.cityid= :cityid ");
			querier.setParam("cityid", cityid);
		}
		if(!SystemCfg.isDataAdminUser(loginUser)){
			querier.append(" and p.gardenid in (select distinct v.wyGarden.gardenid from SysUserGardenRel v where v.sysUser.userid=:userid)");
			querier.setParam("userid", loginUser.getUserid());
		}
		if(StringUtils.isNotEmpty(gardenName)){
			gardenName = gardenName.replace("'", "");
			querier.append(" and (p.gardenName like :gardenName) ");
			querier.setParam("gardenName", "%"+gardenName+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by p.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	
	/**
	 * 添加小区
	 * @param garden
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyGarden garden, LoginUser loginUser, HttpServletRequest request){

		Date now = new Date();
		garden.setCreateTime(now);
		garden.setUpdateTime(now);
		garden.setCreateBy(loginUser.getLogin_name());
		garden.setUpdateBy(loginUser.getLogin_name());
		
		this.save(garden);
		
		return null;
	}
	
	
	/**
	 * 修改小区信息
	 * @param garden
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyGarden garden, LoginUser loginUser, HttpServletRequest request){
		WyGarden old = getSafeEntity(garden.getGardenid());
		if(old==null){
			return new SysError("参数错误");
		}
		String fields = "gardenCode,gardenName,address,leader,linkman,phone,roomNum,multilayerNum,highriseNum," +
				"area,buildingArea,publicArea,greenArea,roadArea,carportDesignArea,carportArea,carportNum,remark";
		BeanUtil.setTargetFields(old, garden, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyGarden> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		return null;
	}
	
	
	
	/**
	 * 删除小区信息
	 * @param gardenids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] gardenids, LoginUser loginUser, HttpServletRequest request){
		for(String gardenid : gardenids){
			WyGarden garden = get(gardenid);
			if(garden==null){
				continue;
			}
			Object obj = getByHql("select buildingid from WyBuilding where wyGarden.gardenid=?", gardenid);
			if(obj!=null){
				return new SysError("小区 “"+garden.getGardenName()+"” 下有楼宇信息，不能删除.");
			}
			
			this.delete(garden);
		}
		return null;
	}
	
	
}
